package top.kaoshanji.ch05;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Scanner;

/**
 * 代码清单 ch05-05：打印一个类的全部信息(5.7.4节)
 * author kaoshanji
 * ^_^
 * create time 2024/11/26 17:47
 */
public class ReflectionTest {

    private static final Logger logger = LogManager.getLogger(ReflectionTest.class);

    public static void main(String[] args) throws ClassNotFoundException {
        logger.info("...代码清单 ch05-05：打印一个类的全部信息(5.7.4节)...");

        // 读取控制台输入的类名
        var in = new Scanner(System.in);
        System.out.println("Enter class name (e.g. java.util.Date): ");
        String name = in.next();

        // 打印类名和超类
        Class cl = Class.forName(name);
        Class supercl = cl.getSuperclass();
        String modifiers = Modifier.toString(cl.getModifiers());
        if (modifiers.length() > 0) {
            System.out.print(modifiers + " ");
        }

        System.out.print("class " + name);
        if (supercl != null && supercl != Object.class) {
            System.out.print(" extends " + supercl.getName());
        }


        System.out.print("\n{\n");
        printConstructors(cl);
        System.out.println();
        printMethods(cl);
        System.out.println();
        printFields(cl);
        System.out.println("}");

    }


    /**
     * 打印本类全部的构造器
     * @param cl Class
     */
    public static void printConstructors(Class cl) {
        logger.info("...打印本类全部的构造器...");

        Constructor[] constructors = cl.getDeclaredConstructors();

        for (Constructor c: constructors) {
            String name = c.getName();
            System.out.print("   ");

            // 修饰符
            String modifiers = Modifier.toString(c.getModifiers());
            if (modifiers.length() > 0) {
                System.out.print(modifiers + " ");
            }
            System.out.print(name + "(");

            // 打印参数类型
            Class[] paramTypes = c.getParameterTypes();
            for (int j = 0; j < paramTypes.length; j++) {
                if (j > 0) {
                    System.out.print(", ");
                }

                System.out.print(paramTypes[j].getName());

            }
            System.out.println(");");

        }
    }

    /**
     * 打印本类的全部方法
     * @param cl Class
     */
    public static void printMethods(Class cl) {
        logger.info("...打印本类的全部方法...");

        Method[] methods = cl.getDeclaredMethods();

        for (Method m: methods) {
            Class retType = m.getReturnType();
            String name = m.getName();

            System.out.print("   ");

            // 打印修饰符、返回类型和方法名称
            String modifiers = Modifier.toString(m.getModifiers());
            if (modifiers.length() > 0) {
                System.out.print(modifiers + " ");
            }
            System.out.print(retType.getName() + " " + name + "(");

            // 打印参数类型
            Class[] paramTypes = m.getParameterTypes();
            for (int j = 0; j < paramTypes.length; j++) {
                if (j > 0) {
                    System.out.print(", ");
                }
                System.out.print(paramTypes[j].getName());
            }

            System.out.println(");");

        }
    }


    /**
     * 打印本类的字段
     * @param cl Class
     */
    public static void printFields(Class cl) {
        logger.info("...打印本类的字段...");

        Field[] fields = cl.getDeclaredFields();

        for (Field f: fields) {
            Class type = f.getType();
            String name = f.getName();
            System.out.print("   ");
            String modifiers = Modifier.toString(f.getModifiers());
            if (modifiers.length() > 0) {
                System.out.print(modifiers + " ");
            }
            System.out.println(type.getName() + " " + name + ";");
        }

    }



}
